package cn.itsource.system.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.system.domain.DictionaryType;
import cn.itsource.system.query.DictionaryTypeQuery;
import cn.itsource.system.service.IDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionaryType")
@Api(value = "数据字典类型API",description = "数据字典类型相关CRUD")
public class DictionaryTypeController {

    @Autowired
    private IDictionaryTypeService dictionaryTypeService;

    @GetMapping("/{id}")
    public DictionaryType queryById(@ApiParam(name="ID属性",required = true)@PathVariable Long id){
        return dictionaryTypeService.queryById(id);
    }

    @GetMapping
    public List<DictionaryType> queryAll(){
        return dictionaryTypeService.queryAll();
    }

    @PostMapping
    public PageList<DictionaryType> queryPage(@RequestBody DictionaryTypeQuery typeQuery){
        return dictionaryTypeService.queryPage(typeQuery);
    }

    @DeleteMapping
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            dictionaryTypeService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("操作失败");
        }
    }

    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody DictionaryType dictionaryType){
        try {
            if (dictionaryType.getId() == null){
                dictionaryTypeService.add(dictionaryType);
            }else {
                dictionaryTypeService.update(dictionaryType);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
        return AjaxResult.me().setSuccess(true).setMessage("操作成功");
    }
}
